package hcs.data.source.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import hcs.data.source.api.dao.newSystem.UserinfoAreainfoNewDao;
import hcs.data.source.api.dao.oldSystem.UserinfoAreainfoDao;

import hcs.data.source.api.entity.newSystem.UserinfoAreainfoEntity;
import hcs.data.source.api.entity.oldSystem.UserinfoAreainfoOldEntity;
import hcs.data.source.api.service.DataSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chaoshen
 * @data 2019/12/13 0013
 */
@Slf4j
@Service
public class DataSourceServiceImpl implements DataSourceService {

    @Autowired
    UserinfoAreainfoNewDao userinfoAreainfoNewDao;

    @Autowired
    UserinfoAreainfoDao userinfoAreainfoDao;

    @Value("${project.userid}")
    private String userId;


    @Override
    @Transactional
    public boolean synchro() {
        //查询新系统数据
        QueryWrapper<UserinfoAreainfoEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("fUserID",userId);
        queryWrapper.between("fTime", LocalDateTime.now().minusMinutes(10),LocalDateTime.now());
        log.info("master请求数据:{}", JSON.toJSONString(queryWrapper.getCustomSqlSegment()));
        List<UserinfoAreainfoEntity> newList = userinfoAreainfoNewDao.selectList(queryWrapper);
        log.info("master返回数据:{}", JSON.toJSONString(newList));
        int count = 0;
        if(newList.size()>0){
            for(UserinfoAreainfoEntity entity:newList){
                //同步新系统数据
                UserinfoAreainfoOldEntity old = new UserinfoAreainfoOldEntity();
                BeanUtils.copyProperties(entity,old);
                old.setFid("99999999909");
                int num = userinfoAreainfoDao.insert(old);
                count += num;
            }
            //long 可以 equals
            if (count == newList.size()) {
                return true;
            }
        }
        return false;
    }

}
